﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheOutLetLibrary.Dominio;
using System.Data.OleDb;

namespace TheOutLetLibrary.Infraestrutura {
    public class Accessk_brandDAO : BaseDAO, IGenericDAO<k_brand> {

        public Accessk_brandDAO(OleDbTransaction transacao)
            : base(transacao) {
        }

        public int Inclua(k_brand entity) {

            OleDbCommand comando = GetCommand();
            comando.CommandText =
                " INSERT INTO k_brand (" +
                " Id, BrandName, Link) " +
                " VALUES (" +
                " @Id, @BrandName, @Link) ";
            comando.Parameters.Add("@Id", OleDbType.Integer).Value = entity.Id;
            comando.Parameters.Add("@BrandName", OleDbType.VarChar).Value = entity.BrandName;
            comando.Parameters.Add("@Link", OleDbType.VarChar).Value = entity.Link;
            comando.ExecuteNonQuery();

            comando.CommandText = "SELECT @@IDENTITY";
            entity.COD = Convert.ToInt32(comando.ExecuteScalar());
            return entity.COD;
        }

        public k_brand Ache(int Id) {

            OleDbCommand comando = GetCommand();
            comando.CommandText = "SELECT * FROM k_brand WHERE Id = @Id";
            comando.Parameters.Add("@Id", OleDbType.Integer).Value = Id;
            OleDbDataReader reader = comando.ExecuteReader();
            if(reader.HasRows) {
                reader.Read();
                return new k_brand {
                    Id = Id,
                    BrandName = reader.GetString(reader.GetOrdinal("BrandName")),
                    Link = reader.GetString(reader.GetOrdinal("Link")),
                    COD = reader.GetInt32(reader.GetOrdinal("COD"))
                };
            } else {
                return null;
            }

        }
    }
}
